import { NextRequest, NextResponse } from 'next/server'
import { getUserFavorites, getFavorites } from '@/lib/database'

// 获取收藏列表
export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url)
    const userId = searchParams.get('userId')

    let favorites
    if (userId) {
      // 获取特定用户的收藏
      favorites = await getUserFavorites(userId)
    } else {
      // 获取所有收藏（用于统计）
      favorites = await getFavorites()
    }

    return NextResponse.json({
      success: true,
      data: favorites
    })
  } catch (error) {
    console.error('获取收藏失败:', error)
    return NextResponse.json(
      { error: '服务器内部错误' },
      { status: 500 }
    )
  }
}
